package com.zhxh.nzpos.sell;

import java.util.ArrayList;

import android.text.Editable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.zhxh.nzpos.R;
import com.zhxh.nzpos.model.Goods;
import com.zhxh.nzpos.model.Sellout;

public class DealListAdapter extends BaseAdapter {

	private ArrayList<Sellout> items;
	private ArrayList<Goods> goodsList;
	private LayoutInflater inflater;
	private ItemDeleteCallback deleteCallback;
	
	public DealListAdapter(LayoutInflater inflater, ArrayList<Sellout> dealItems, ArrayList<Goods> goodsList, ItemDeleteCallback deleteCallback) {
		this.items = dealItems;
		this.goodsList = goodsList;
		this.inflater = inflater;
		this.deleteCallback = deleteCallback;
	}
	
	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.deal_list_item, null);
			holder = new ViewHolder();
			holder.identifier = (TextView) convertView.findViewById(R.id.goods_identifier_et);
			holder.name = (TextView) convertView.findViewById(R.id.goods_name_et);
			holder.sum = (EditText) convertView.findViewById(R.id.goods_number_et);
			holder.sum.setOnEditorActionListener(new OnEditorActionListener() {
				public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
					Log.i("input", "action:"+actionId);
//					if (actionId == EditorInfo.IME_ACTION_DONE) {
						Editable e = ((EditText)v).getText();
						if (e != null) {
							try {
								int num = Integer.valueOf(e.toString());
								Sellout order = items.get(position);
								order.setCount(num);
								notifyDataSetChanged();
							} catch (NumberFormatException e1) {
								e1.printStackTrace();
							}
						}
//					}
					return false;
				}
			});
			holder.unitPrice = (EditText) convertView.findViewById(R.id.goods_unit_price_et);
			holder.money = (EditText) convertView.findViewById(R.id.goods_sum_money_et);
			holder.discount = (EditText) convertView.findViewById(R.id.goods_discount_et);
			holder.deleteBtn = (Button) convertView.findViewById(R.id.goods_delete_btn);
			holder.bulkSale = (CheckBox) convertView.findViewById(R.id.bulk_sale_cb);
			holder.bulkSale.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					Sellout sellout = items.get(position);
					Goods goods = goodsList.get(position);
					if (isChecked) {
						sellout.setPrice(goods.getPrice2());
						sellout.setUnits(goods.getUnits2());
					} else {
						sellout.setPrice(goods.getPrice1());
						sellout.setUnits(goods.getUnits2());
					}
					notifyDataSetChanged();
				}
			});
			holder.deleteBtn.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (deleteCallback != null) {
						deleteCallback.onDelete(position);
					}
				}
			});
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		Sellout item = items.get(position);
		if (item != null) {
			holder.identifier.setText(item.getGoodId());
			holder.name.setText(item.getGoodsName());
			holder.sum.setText(""+item.getCount());
			holder.unitPrice.setText(""+item.getPrice());
			holder.money.setText(""+item.getTotal());
			// TOTO 折扣
//			holder.discount.setText(""+item.getDiscount());
		}
		return convertView;
	}

	static class ViewHolder {
		TextView identifier;
		TextView name;
		EditText sum;
		EditText unitPrice;
		EditText money;
		EditText discount;
		CheckBox bulkSale;
		Button  deleteBtn;
	}
}
